package com.controller;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.Address;
import com.entity.Cake;
import com.entity.Consumer;
import com.entity.Order;
import com.entity.ShopCart;
import com.fasterxml.jackson.databind.Module;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.servers.AddressServers;
import com.servers.ConsumerServers;
import com.servers.OrderServers;
import com.servers.ShopCarService;
import com.util.DateUtil;
import com.util.OrderUtil;




@Controller  
public class sum {
	@Autowired
	private ShopCarService shopCarService;
	@Autowired
	private OrderServers  orderService;
	@Autowired
	private ConsumerServers  consumerServers;
	@Autowired
	private AddressServers addressServers;
	@RequestMapping("/sendOrder")
	public  String  sumOrder(double sumCount,String sendAddress,String orderCusomer,String orderTel ,int hiddenCid,ModelMap modelmap){
		System.out.println(sumCount+"=="+sendAddress+"===="+orderCusomer+"===="+orderTel+"===="+hiddenCid);
		List<ShopCart> slist=shopCarService.getShopCarInfoByStatus(2,hiddenCid);
		Consumer consumer=consumerServers.getConsumerInfo(hiddenCid, sendAddress);
		Address  address =addressServers.getAddressName(hiddenCid, sendAddress);
		Random ra=new Random();
		double orderOrder=ra.nextInt(1000+1);
		for (ShopCart shopCart : slist) {
			Order  order=new Order();
			//生成一个随机的订单编号
			order.setOnum(shopCart.getScnum());
			//order.setCake(new Cake(hiddenCid));
			order.setConsumer(new Consumer(hiddenCid));
			order.setCake(new Cake(shopCart.getScname()));
			order.setOaddress(sendAddress);
			order.setOcost(shopCart.getScnum()*shopCart.getScPrice());//小计
			order.setOdiscount(0);
			order.setOnum(shopCart.getScnum());
			order.setOunitPrice(shopCart.getScPrice());//单价
			order.setOdiscount(orderOrder);// 插入一个随机的订单编号
			order.setOdelDate(DateUtil.dateTimeToString(new Date()));
			int num=orderService.InsertOrderInfo(order);
			//System.out.println("+========="+num);
			//将生成订单后购物车的状态设置为0
			ShopCart shop =new ShopCart();
			shop.setConsumer(new Consumer(hiddenCid));;
			shop.setScname(shopCart.getScname());
			int status=shopCarService.updateShopCartStatus(shop);
			System.out.println("-----------"+status);
			modelmap.addAttribute("sumCount", sumCount);
			modelmap.addAttribute("address", sendAddress);
			modelmap.addAttribute("orderCusomer", orderCusomer);
			modelmap.addAttribute("orderTel", orderTel);
		}
	   return "sum";
	}
	@RequestMapping("/testOrer")
	public String ordersumDemo(String data){
		System.out.println("====ordersumDemo======"+data);
		Gson gson=new Gson();
		Map<String,List<OrderUtil>> fromJson=gson.fromJson(data, new  TypeToken<Map<String, List<OrderUtil>>>(){}.getType() );
		List<OrderUtil> olist=fromJson.get("datas");
		for (OrderUtil orderUtil : olist) {
			int num =orderService.updateOrder(orderUtil.getOrderId(), 3);
			//System.out.println(num);
		}
		//System.out.println("**************"+fromJson);
		return "test";
	}
}
